package com.zqwh.yj.sdk.utils;

import android.content.Context;

import com.zqwh.yj.sdk.obj.AntConfig;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;

/**
 * Created by eason.yang on 2018/8/23.
 */

public class SerializeUtils {
    public static final String USER_SERIALIZE_PATH = "/ant_config.ser";

    public static void serializeUsers(Context context, List<AntConfig> antConfigs) {
        try {
            ObjectOutputStream out;
            out = new ObjectOutputStream(new FileOutputStream(context.getFilesDir() + USER_SERIALIZE_PATH));
            out.writeObject(antConfigs);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<AntConfig> unSerializeUsers(Context context) {
        List<AntConfig> antConfigs = null;
        try {
            File file = new File(context.getFilesDir() + USER_SERIALIZE_PATH);
            if (file.exists()) {
                ObjectInputStream in;
                in = new ObjectInputStream(new FileInputStream(context.getFilesDir() + USER_SERIALIZE_PATH));
                antConfigs = (List<AntConfig>) in.readObject();
                in.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return antConfigs;
    }
}
